package com.jiruan.invoicemanagementsystem.dao.Imp;

import com.jiruan.invoicemanagementsystem.dao.IAddressDao;
import com.jiruan.invoicemanagementsystem.pojo.Address;
import com.jiruan.invoicemanagementsystem.pojo.Email;
import com.jiruan.invoicemanagementsystem.util.DBHelper;
import com.jiruan.invoicemanagementsystem.util.PageHelper;

import java.util.ArrayList;
import java.util.Arrays;

public class IAddressDaoImp implements IAddressDao {

    //获取全部地址信息
        public ArrayList<Address> selectAllAddress(){
        String sql="select *from address";
       return DBHelper.currencySqlQuery(Address.class,sql);

    }




    //根据id设置邮箱地址信息
    public int updateEmailAddressById(Address address){
            String sql="update email set addressDetail=? where id=?";
            return DBHelper.currencySqlUpdate(sql,address.getAddressDetail(),address.getId());
    }

    //设置默认邮箱信息
    public int updateAddressById(Address address) {
       String sql="update address set defaultStatus = case when id = ? then 'A' else 'B' end";
       return DBHelper.currencySqlUpdate(sql,address.getId());
    }

    //获取默认的邮寄地址信息
    @Override
    public Address selectDefaultAddress() {

        String sql = "select * from address where defaultStatus = 'A'";
        return (Address) DBHelper.currencySqlQuery(Address.class,sql).getFirst();
    }

    //根据条件信息查找并将地址设置为默认邮寄地址
    public int updateDefaultAddress(Address address){


       StringBuilder sql=new StringBuilder("update from address where 1=1");
        ArrayList<Object>list=new ArrayList<>();

        if (address.getEnterpriseId()!=null){
            sql.append("and enterpriseId=?");
            list.add(address.getEnterpriseId());
        }
        if (address.getAddressee()!=null){
            sql.append("and addressee=?");
            list.add(address.getAddressee());
        }if (address.getPhone()!=null){
            sql.append("and phone=?");
            list.add(address.getPhone());
        }if (address.getArea()!=null){
            sql.append("and area=?");
            list.add(address.getArea());
        }  if (address.getAddressDetail()!=null){
            sql.append("and addressDetail=?");
            list.add(address.getAddressDetail());
        }
        if (address.getDefaultStatus()!=null){
            sql.append("and defaultStatus=?");
            list.add(address.getDefaultStatus());
        }

        return DBHelper.currencySqlUpdate(sql.toString(),list.toArray());
    }

    //添加一条新的地址信息
    public int insertNewAddress(Address address) {
        String sql = "insert into address values(null,?,?,?,?,?)";
        Object[] obj = {address.getEnterpriseId(), address.getAddressee(), address.getPhone(),
                address.getArea(), address.getAddressDetail(), address.getDefaultStatus()};
        return DBHelper.currencySqlUpdate(sql, obj);

    }



//分页邮箱地址信息，用于选择按钮
    public ArrayList<Address> selectPageAllAddress(PageHelper ph){
        String sql ="select *from address limit ?,?";

        int index=(ph.getPageIndex()-1)*ph.getShowNumber();
//        System.out.println(index);
        return DBHelper.currencySqlQuery(Address.class, sql,index, ph.getShowNumber());
    }

    //获得所有邮箱条数
    public int queryAddressCount(){
        String sql ="select count(*) from address";
        return DBHelper.currencySqlQuery(sql);
    }


}





